package com.willren.usharejni;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import com.willren.usharejni.UshareJni.UShareStateInterface.UShareStateType;

import android.content.Context;

public class UshareJni {

	private static UshareJni objUShareManager = null;

	private String ifaceName;
	private String ipAddress;
	private boolean isUShareStarted;

	public interface UShareStateInterface {
		public enum UShareStateType {
			UsState_Started, UsState_Stoped, UsState_Error
		}

		public void ushareStateChanged(UShareStateType state);
	}

	public static UshareJni getUShareManagerInstance(Context context) {
		if (null == objUShareManager) {
			objUShareManager = new UshareJni();
		}

		return objUShareManager;
	}

	private UshareJni() {
		isUShareStarted = false;
	}

	public void startUShare(String serverName, String contentDir,
			UShareStateInterface callback) {

		getIface();
		
		final String finalIfaceName = ifaceName;
		final String finalServerNamer = serverName;
		final String finalContentDir = contentDir;
		final UShareStateInterface finalCallback = callback;

		new Thread(new Runnable() {
			@Override
			public void run() {

				int retInt = startServer(finalIfaceName, finalServerNamer, finalContentDir);
				if (0 == retInt) {
					isUShareStarted = true;
					if (null != finalCallback) {
						finalCallback.ushareStateChanged(UShareStateType.UsState_Started);
					}
				} else {
					isUShareStarted = false;
					if (null != finalCallback) {
						finalCallback.ushareStateChanged(UShareStateType.UsState_Error);
					}
				}
			}
		}).start();
	}

	public void stopUShare(UShareStateInterface callback) {

		final UShareStateInterface finalCallback = callback;

		new Thread(new Runnable() {
			@Override
			public void run() {
				stopServer();
				isUShareStarted = false;

				if (null != finalCallback) {
					finalCallback.ushareStateChanged(UShareStateType.UsState_Stoped);
				}
			}
		}).start();
	}
	
	public boolean isUShareStarted() {
		return isUShareStarted;
	}
	
	public String getIpAddress() {
		return ipAddress;
	}

	private void getIface() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();

					if (false == inetAddress.isLoopbackAddress()) {
						ifaceName = intf.getName();
						ipAddress = inetAddress.toString();
						break;
					}
				}
			}
		} catch (SocketException ex) {
		}
	}

	private native int startServer(String ifaceName, String serverName,
			String contentDir);

	private native int stopServer();

	static {
		System.loadLibrary("ushare-jni");
	}
}
